<?php

class Student
{
	private $id;
	private $student_nr;
	private $voornaam;
	private $achternaam;
	private $tussenvoegsel;
	private $opleiding;
	private $actief;
	
	private $stages;
	
	private $wsdl;
	private $sugar_client;
	
	/* Constructor */
	public function __construct($arg_wsdl)
	{
		//echo('Student->Constructor('.$arg_wsdl.')');
		$this->wsdl = $arg_wsdl;
		$this->sugar_client = new nusoapclient($this->wsdl, true);
	}
	
	/* InitData */
	public function setData($arg_student_nr, $arg_voornaam, $arg_achternaam, $arg_tussenvoegsel, $arg_opleiding)
	{
		//echo('<br />Student->setData('.$arg_student_nr.', '.$arg_voornaam.', '.$arg_achternaam.', '.$arg_tussenvoegsel.', '. $arg_opleiding.')');
									   
		$this->id = NULL;
		$this->student_nr = $arg_student_nr;
		$this->voornaam = $arg_voornaam;
		$this->achternaam = $arg_achternaam;
		$this->tussenvoegsel = $arg_tussenvoegsel;
		$this->opleiding = $arg_opleiding;
		$this->actief = 'Actief';
		$this->stages = NULL;
	}
	
	/* Set Methoden */
	public function setId($arg_id)
	{
		//echo('<br />Student->setId('.$arg_id.')');
		$this->id = $arg_id;
	}
	
	public function setStudent_nr($arg_student_nr)
	{
		//echo('<br />Student->setStudent_nr('.$arg_student_nr.')');
		$this->student_nr = $arg_student_nr;
	}
	
	public function setVoornaam($arg_voornaam)
	{
		//echo('<br />Student->setVoornaam('.$arg_voornaam.')');
		$this->voornaam = $arg_voornaam;
	}
	
	public function setAchternaam($arg_achternaam)
	{
		//echo('<br />Student->setAchternaam('.$arg_achternaame.')');
		$this->achternaam = $arg_achternaam;
	}
	
	public function setTussenvoegsel($arg_tussenvoegsel)
	{
		//echo('<br />Student->setTussenvoegsel('.$arg_tussenvoegsel.')');
		$this->tussenvoegsel = $arg_tussenvoegsel;
	}
	
	public function setOpleiding($arg_opleiding)
	{
		//echo('<br />Student->setOpleiding('.$arg_opleiding.')');
		$this->opleiding = $arg_opleiding;
	}
	
	public function setActief($arg_actief)
	{
		//echo('<br />Student->setActief('.$arg_actief.')');
		$this->actief = $arg_actief;
	}
	
	public function setStages($arg_stages)
	{
		//echo('<br />Student->setStages('.$arg_stages.')');
		$this->stages = $arg_stages;
	}
	
	/* Get Methoden */
	public function getId()
	{
		//echo('<br />Student->getId()');
		return $this->id;
	}
	
	public function getStudent_nr()
	{
		//echo('<br />Student->getStudent_nr()');
		return $this->student_nr;
	}
	
	public function getVoornaam()
	{
		//echo('<br />Student->getVoornaam()');
		return $this->voornaam;
	}
	
	public function getAchternaam()
	{
		//echo('<br />Student->getAchternaam()');
		return $this->achternaam;
	}
	
	public function getTussenvoegsel()
	{
		//echo('<br />Student->getTussenvoegsel()');
		return $this->tussenvoegsel;
	}
	
	public function getOpleiding()
	{
		//echo('<br />Student->getOpleiding()');
		return $this->opleiding;
	}
	
	public function getActief()
	{
		//echo('<br />Student->getActief()');
		return $this->actief;
	}
	
	public function getStages()
	{
		//echo('<br />Student->getStages()');
		return $this->stages;
	}
	
	public function getEmail()
	{
		//echo('<br />Student->getEmail()');
		if($this->voornaam != '' && $this->achternaam != '')
		{
			if($this->tussenvoegsel == '')
			{
				return strtolower($this->voornaam.'.'.$this->achternaam.'@student.hu.nl');
			}
			else
			{
				$formattedTussenvoegsel = str_replace (" ", "", $this->tussenvoegsel);
				return strtolower($this->voornaam.'.'.$formattedTussenvoegsel.$this->achternaam.'@student.hu.nl');
			}
		}
		return '';
	}
	
	public function getFullName()
	{
		//echo('<br />Student->getFullName()');
		if($this->voornaam != '' && $this->achternaam != '')
		{
			if($this->tussenvoegsel =='')
			{
				return $this->voornaam.' '.$this->achternaam;
			}
			else
			{
				return $this->voornaam.' '.$this->tussenvoegsel.' '.$this->achternaam;
			}
		}
		return '';
	}
	
	/* Other */
	public function save()
	{
		//echo('<br />Student->save()');
		
		$session_id = $_SESSION['session'];
		$user_hash = '';
		
		$set_entry_params = array(
	    	'session' => $session_id,
	        'module_name' => 'st_Student',
	        'name_value_list'=>array(
									array('name'=>'id','value'=>$this->getId()),
	                           		array('name'=>'student_nr','value'=>$this->getStudent_nr()),
									array('name'=>'name','value'=>$this->getVoornaam()),
									array('name'=>'last_name','value'=>$this->getAchternaam()),
									array('name'=>'betweentf','value'=>$this->getTussenvoegsel()),
									array('name'=>'education','value'=>$this->getOpleiding()),
									array('name'=>'active','value'=>$this->getActief())
	                          		)
	                          );
		
		$result = $this->sugar_client->call('set_entry',$set_entry_params);

		if($result['error']['name']== "No Error")
		{
			$this->id = $result['id'];

			$user_id = getUserId($this->getId());
			
			$pass = generatePassword(8);
			$md5pass = strtolower(md5($pass));
			
			$actief;
			
			if($this->getActief() == 'Actief')
			{
				$actief = 'Active';
			}
			else
			{
				$actief = 'Inactive';
			}
			
			$set_entry_params = array
			(
				'session' => getAdminSession(),
				'module_name' => 'Users',
				'name_value_list'=>array
				(
					array('name'=>'id','value'=>$user_id),
					array('name'=>'user_name','value'=>$this->getEmail()),
					array('name'=>'user_hash','value'=>$md5pass),
					array('name'=>'status','value'=>$actief),
					array('name'=>'last_name','value'=>'Student')
				)
			);
	
			$result = $this->sugar_client->call('set_entry', $set_entry_params);
			
			if($result['error']['name'] == "No Error")
			{
				if($this->getActief()=='Actief')
				{
					sendConfirmationMail($this, $pass, 'change');
				}
				
				return $this->getId();
			}
		}
		else
		{
			return $result['error']['name'];
		}
	}
	
	public function CreateStudent()
	{
		//echo('Student->CreateStudent()');
		$session_id = $_SESSION['session'];
		
		$studentCreated = false;
		$userCreated = false;
		$relatieCreated = false;
		
		//TODO: add validation on studentnr
		//Create Student
		$set_entry_params = array(
	    	'session' => $session_id,
	        'module_name' => 'st_Student',
	        'name_value_list'=>array(
	                           	 	array('name'=>'id','value'=>NULL),
	                           		array('name'=>'student_nr','value'=>$this->getStudent_nr()),
									array('name'=>'name','value'=>$this->getVoornaam()),
									array('name'=>'last_name','value'=>$this->getAchternaam()),
									array('name'=>'betweentf','value'=>$this->getTussenvoegsel()),
									array('name'=>'education','value'=>$this->getOpleiding()),
									array('name'=>'active','value'=>$this->getActief())
	                          		)
	                          );
		
		$result = $this->sugar_client->call('set_entry',$set_entry_params);

		if($result['error']['name']== "No Error")
		{
			$this->id = $result['id'];
			$studentCreated = true;
		}
		else
		{
			return $result['error']['name'];
		}
		
		$pass = generatePassword(8);
		$md5pass = strtolower(md5($pass));
		
		$user_id = createUser('Student', $this->getEmail(), $md5pass, $this->getId());
		
		if($user_id != NULL)
		{
			$userCreated = true;
			
			$role_id = getRole('Student');
			
			if($role_id != NULL)
			{
				$relatieCreated = createRelatie('Roles', $role_id, 'Users', $user_id);
			}
		}
			
		if($studentCreated && $userCreated && $relatieCreated)
		{
			sendConfirmationMail($this, $pass, 'new');
			return $this->getId();
		}
		
		/* For debug purposes */
		//echo('<br />Role_id: '.$role_id);
		//echo('<br />Password: '.$pass);
		//echo('<br />MD5 Password: '.$md5pass);
	}
}

?>